package com.bms.admin.bussiness.member.web;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bms.admin.base.index.model.NowUser;
import com.bms.admin.config.annotation.LoginUser;
import com.bms.auth.annotation.RequiresPermissions;
import com.bms.common.base.common.bean.Result;
import com.bms.common.base.web.BaseController;
import com.bms.common.bussiness.member.entity.Member;
import com.bms.common.bussiness.member.entity.MemberInfo;
import com.bms.common.bussiness.member.service.IMemberInfoService;
import com.bms.common.bussiness.member.service.IMemberService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author JIANG
 */
@RestController
@RequestMapping("member")
@Api(value="Member-Api",tags="会员管理接口")
public class MemberController extends BaseController{
	
	@Autowired
	private IMemberService memberService;
	
	@Autowired
	private IMemberInfoService memberInfoService;
	
	
	
	@ApiOperation("会员列表")
	@GetMapping("list")
	@RequiresPermissions("member:list")
    public Result list(@ApiIgnore @LoginUser NowUser nowUser,
    				  @RequestParam(defaultValue = "1",required=false)Integer pageNum,
    				  @RequestParam(defaultValue = "20",required=false)Integer pageSize,
    				  @RequestParam(name="phone",required=false) String phone) {
		EntityWrapper<Member> ew = new EntityWrapper<>();
		ew.orderBy("create_time", false);
		if(StringUtils.isNotBlank(phone)){
			ew.like("phone", phone);
		}
		Page<Map<String, Object>> page = memberService.selectMapsPage(new Page<Member>(pageNum,pageSize), ew);
		for(int i=0,size=page.getRecords().size();i<size;i++){
			Map<String, Object> map = page.getRecords().get(i);
			Object openId = map.get("openId");
			if(null != openId){
				EntityWrapper<MemberInfo> mew = new EntityWrapper<>();
				mew.eq("open_id", openId);
				int cardCount = memberInfoService.selectCount(mew);
				map.put("cardCount", cardCount);
			}
		}
		return json(page);
    }
	
	
	@ApiOperation("锁定用户")
	@PostMapping("locking")
	@RequiresPermissions("member:locking")
    public Result locking(@RequestParam("memberId") Long memberId,@RequestParam("status") Integer status) {
		if(!Integer.valueOf(1).equals(status) && !Integer.valueOf(2).equals(status)){
			Result error = error();
			error.setMessage("参数异常");
			return error; 
		}
		Member member = new Member();
		member.setId(memberId);
		member.setStatus(status);
		boolean success = memberService.updateById(member);
		if(success){
			return success();
		}
		return error();
    }
	
	
}
